﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace RC1.admin
{
    public partial class delete_user : System.Web.UI.Page
    {
        int user_id;

        protected void Page_Load(object sender, EventArgs e)
        {
            user_id = Convert.ToInt32(Request.QueryString["id"]);
            populatePage();
        }

        private void populatePage()
        {
            tsu2011db4Entities1 db = new tsu2011db4Entities1();
            var user = (from u in db.lpusers
                        where u.user_id == user_id
                        select u).SingleOrDefault();

            if (user != null)
            {
                if (user.user_level == 1)
                {
                    checkTeacher(user.user_id);
                }
                else
                {
                    checkStudnet(user.user_id);
                }
            }
        }

        private void checkTeacher(int id)
        {
            tsu2011db4Entities1 db = new tsu2011db4Entities1();
            var cl = (from c in db.classes
                      where c.user_id == id
                      select c);

            if (cl.Count() > 0)
            {
                String temp = "<h3>Lærari kann ikki strikast enn</h3>";
                temp += "<p>Lærarin er í " + cl.Count() + " flokkum<br/>";
                temp += "Tað er neyðugt at seta aðrar lærarir at eiga flokkanir</p>";
                temp += "<br/>";
                int count = 1;
                foreach (var c in cl)
                {
                    temp += "<p><a href='edit_class.aspx?id=" + c.class_id + "&p=" + user_id + "'>" + count + ". " +c.class_name + "</a></p>";
                    count++;
                }
                temp += "<br/>";
                temp += "<p><a href='teacher.aspx'>Angra</a></p>";
                Label1.Text = temp;
            }
            else
            {
                var user = (from u in db.lpusers
                            where u.user_id == id
                            select u).SingleOrDefault();

                var ui2 = (from ui in db.user_info
                           where ui.user_id == id
                           select ui).SingleOrDefault();
                db.lpusers.DeleteObject(user);
                db.user_info.DeleteObject(ui2);
                db.SaveChanges();
                Response.Redirect("teacher.aspx");
            }
        }

        private void checkStudnet(int id)
        {
            tsu2011db4Entities1 db = new tsu2011db4Entities1();

            var user = (from u in db.lpusers
                        where u.user_id == id
                        select u).SingleOrDefault();

            var ui2 = (from ui in db.user_info
                       where ui.user_id == id
                       select ui).SingleOrDefault();

            var att = (from a in db.attendings
                       where a.user_id == id
                       select a);

            var sub = (from s in db.submissions
                       where s.user_id == id
                       select s);  
            if (Request.QueryString["c"] != null)
            {
                foreach (var sub2 in sub)
                {
                    var cor = (from cs in db.corrected_submission
                               where cs.submission_id == sub2.submission_id
                               select cs).SingleOrDefault();

                    var file = (from f in db.files
                                where f.file_id == sub2.file_id
                                select f).SingleOrDefault();
                    if (file != null)
                    {
                        db.files.DeleteObject(file);
                    }
                    if (cor != null)
                    {
                        db.corrected_submission.DeleteObject(cor);
                    }
                    db.submissions.DeleteObject(sub2);
                }
                foreach (var at in att)
                {
                    db.attendings.DeleteObject(at);
                }
                db.user_info.DeleteObject(ui2);
                db.lpusers.DeleteObject(user);
                db.SaveChanges();
                Response.Redirect("student.aspx");
            }
            else
            {
                String temp = "<h3>Skal " + ui2.user_info_first_name + " " + ui2.user_info_last_name + "  strikast?</h3>";
                temp += "<p>Næmingurin er í " + att.Count() + " flokkum</p>";
                temp += "<p>Næmingurin hevur " + sub.Count() + " uppgávur</p>";
                temp += "<br/>";
                temp += "<p>Næmingurin verður eisini strikaður úr flokkunum allar uppgávurnar verða eisini strikaðar</p>";
                temp += "<p>Skal næmingur strikast ?</p>";
                temp += "<p><a href='delete_user.aspx?id=" + id + "&c=y'>Ja</a> <a href='student.aspx'>Nei</a></p>";
                Label1.Text = temp;
            }            
        }
    }
}
